﻿@using KRM.Core
@using KRM.Core.Library
@model tblTicket

@{
    ViewBag.Title = "TicketDetails";
    var db = new KRMEntities();
     var dbLocal=new KRMLocalEntities();
    var ListTag = ViewBag.ListTag as List<KRM.Core.tblTag>;
    var ListTicketRelations = ViewBag.ListTicketRelations as List<tblTicketRelation>;
    var ListAudit = ViewBag.ListAudit as List<tblTicketAudit>;
    var ListFirstEmail = ViewBag.ListFirstEmail as List<tblEmail>;
    var ConversionIDEmailHistory = ViewBag.ConversionIDEmailHistory as Dictionary<int, List<tblEmail>>;
    var OriginalEmail = ViewBag.OriginalEmail as tblEmail;
}
<script src="/js/mail.js"></script>
    

<div id="CreateTaskModal" class="modal fade" tabindex="-1" data-width="800">
            <div style="height: 500px">
                <div style="height: 220px"></div>
                <div class="ajax-loading"></div>
            </div>
</div>

<div id="DivViewRelationObject" class="modal fade" tabindex="-1" data-width="800">
            <div style="height: 500px">
                <div style="height: 220px"></div>
                <div class="ajax-loading"></div>
            </div>
</div>

<div id="DivViewAudit" class="modal fade" tabindex="-1" data-width="800">
            <div style="height: 500px">
                <div style="height: 220px"></div>
                <div class="ajax-loading"></div>
            </div>
</div>
<div id="ViewReplyEmail" class="modal fade" tabindex="-1" data-width="800">
            <div style="height: 500px">
                <div style="height: 220px"></div>
                <div class="ajax-loading"></div>
            </div>
</div>
<input type="hidden" name="ID" value="@Model.TicketID"/>
@Html.Partial("Partial/_MessageResult")

<div class="span5"></div>
<div class="form-horizontal">
    <div class="form-body alert note-info">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <a onclick="ViewAudit(@Model.TicketID)" class="btn blue control-label" style="margin-left:15px;" data-toggle="modal" href="#DivViewAudit">View action history</a>  
                  
                    @if (Model.StatusID == 2)
                    {             //Open           
                    <a onclick="CreateTask(@Model.OriginalEmailID)" class="btn green control-label" style="margin-left:5px;" data-toggle="modal" href="#">Create task</a>
                    <a class="btn btn-info control-label" style="margin-left:5px;" data-toggle="modal" href="/EmailTicket/PendingTicket/?ticketID=@Model.TicketID&UserID=@Users.Current(User.Identity.Name).UserID">Pending</a>
                    <a class="btn dark control-label" style="margin-left:5px;" data-toggle="modal" href="/EmailTicket/SolveTicket/?ticketID=@Model.TicketID&UserID=@Users.Current(User.Identity.Name).UserID">Solved</a>
                    }
                    else if (Model.StatusID == 4 || Model.StatusID == 5)//Solved //Closed
                    {
                    <a class="btn green control-label" style="margin-left:5px;" data-toggle="modal" href="/EmailTicket/StartTicket/?ticketID=@Model.TicketID&UserID=@Users.Current(User.Identity.Name).UserID">Open</a>
                    }
                    else
                    {
                    <a class="btn green control-label" style="margin-left:5px;" data-toggle="modal" href="/EmailTicket/StartTicket/?ticketID=@Model.TicketID&UserID=@Users.Current(User.Identity.Name).UserID">Open</a>                    
                    <a class="btn dark control-label" style="margin-left:5px;" data-toggle="modal" href="/EmailTicket/SolveTicket/?ticketID=@Model.TicketID&UserID=@Users.Current(User.Identity.Name).UserID">Solved</a>
                    }                                                           
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-6">
                <div class="portlet box blue">
                    <div class="portlet-title">
                        <div class="caption"><i class="icon-th-large"></i>Relation<br />
                            @foreach (var item in ListTicketRelations)
                            {                                         
                                if (item.RelationTypeID == 1)
                                {
                                                    
                                    <a onclick="ViewTask(@item.Value)" style="color:white;cursor:pointer;"> 
                                        <label style="margin-top:10px;font-size:15px; color:white;cursor:pointer;">@db.tblRelationTypes.Find(item.RelationTypeID).RelationType: @item.Value</label>
                                    </a>
                                }
                                else
                                {
                                    <a onclick="" data-toggle="modal" href="/OrderView/OrderDetail/?orderRef=@item.Value">
                                        <label style="margin-top:10px;font-size:15px; color:white;cursor:pointer;">@db.tblRelationTypes.Find(item.RelationTypeID).RelationType: @item.Value</label>
                                    </a>    
                                }  
                                <br />
                            }
                        </div>
                        <div class="tools">
                            <a href="javascript:;" class="expand"></a>
                        </div>
                    </div>
                    <div class="portlet-body" style="display: none;">
                            <div class="scroller" style="overflow: hidden; width: auto;">
                                      <table id="ListRelations" class="table table-bordered table-fix tbl-selected table-hover">
                                          <thead>
                                              <tr>
                                                  <th>RelationType</th>
                                                  <th>Value</th>
                                                  <th>View</th>
                                                  <th>Delete</th>
                                              </tr>
                                          </thead>
                                          <tbody>
                                              @foreach (var item in ListTicketRelations)
                                              {
                                                  <tr onclick="ticketrelationdetail('@item.RelationID')" style="cursor: pointer;">
                                                      <td>@db.tblRelationTypes.Find(item.RelationTypeID).RelationType</td>
                                                      <td>@item.Value</td>
                                                      <td style="text-align:center; width:100px;">
                                                          @if (item.RelationTypeID == 1)
                                                          {
                                                              <a  onclick="ViewTask(@item.Value)" class="btn btn-sm blue" style="width:90px;">View Task</a>
                                                          }
                                                          else
                                                          {
                                                              <a target="blank" class="btn btn-sm blue" style="width:90px;" href="/OrderView/OrderDetail/?orderRef=@item.Value">View Order</a>    
                                                          }
                                                      </td>
                                                      <td style="text-align:center; width:50px;">
                                                          <a onclick="javascript:return confirm('Are you sure to delete this ?');" class="btn btn-sm red" href ="/EmailTicket/DeleteTicketRelation/?id=@item.RelationID&UserID=@Users.Current(User.Identity.Name).UserID">Delete</a>
                                                      </td>
                                                  </tr>
                                              }
                                          </tbody>
                                      </table>
                                      <div id="DivTicketRelationDetail">
                                          <form action="/EmailTicket/SaveTicketRelation" id="fr-TicketRelation" method="POST" class="form-horizontal" role="form">
                                              <input type="hidden" name="RelationID" value="0"/>
                                              <input type="hidden" name="TicketID" value="@Model.TicketID"/>
                                              <input type="hidden" name="UserID" value="@Users.Current(User.Identity.Name).UserID"/>
                                              <div class="form-group">
                                                  <label  class="col-md-3 control-label">Type</label>
                                                  <div class="col-md-9">
                                                      @Html.DropDownList("RelationTypeID",null,new {@class="form-control"})
                                                  </div>
                                              </div> 

                                              <div class="form-group">
                                                  <label  class="col-md-3 control-label">Value</label>
                                                  <div class="col-md-9">
                                                      <input id="Value" type="text" name="Value" required="required" class="form-control">                                
                                                  </div>
                                              </div>  
                                    
                                              <button type="submit" id="btnSave" class="btn btn-primary"onclick="" style="float:right;margin-left:10px;">Save</button>                                   
                                              <a id="btnNew" class="btn btn-primary" onclick="ticketrelationdetail(0)" style="float:right;margin-left:10px;">New</a>
                                          </form>
                                      </div>
                                  </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="control-label col-md-4">Subject:</label>
                        <div class="col-md-8">
                            <p class="form-control-static">@Model.Subject</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="control-label col-md-4">Date added:</label>
                        <div class="col-md-8">
                            <p class="form-control-static">@Model.DateAdded</p>
                        </div>
                    </div>
                </div>
                <!--/span-->
           
                <!--/span-->
            </div>
            <!--/row-->
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="control-label col-md-4">Added by user:</label>
                        <div class="col-md-8">
                            <p class="form-control-static">@db.tblUsers.Find(@Model.AddedByUserID).Username</p>
                        </div>
                    </div>
                </div>
                <!--/span-->
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="control-label col-md-4">Assigned to user:</label>
                        <div class="col-md-8">
                            <p class="form-control-static">
                                @if (@Model.AssignedToUserID != null && @Model.AssignedToUserID != 0)
                                {
                                    @db.tblUsers.Find(@Model.AssignedToUserID).Username
                                }
                            </p>
                        </div>
                    </div>
                </div>
            
                <!--/span-->
            </div>
            <!--/row-->        
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="control-label col-md-4">Start date:</label>
                        <div class="col-md-8">
                            <p class="form-control-static">@Model.DateStarted</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="control-label col-md-4">Complete Date:</label>
                        <div class="col-md-8">
                            <p class="form-control-static">@Model.DateCompleted</p>
                        </div>
                    </div>
                </div>

            </div>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="control-label col-md-4">Description:</label>
                        <div class="col-md-8">
                            <p class="form-control-static">@Model.Description</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="control-label col-md-4">Status:</label>
                        <div class="col-md-8">
                            <p class="form-control-static">@db.tblTicketStatus.Find(@Model.StatusID).Status
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="control-label col-md-4">Tag:</label>
                        <div class="col-md-8">
                            @for (int i = 0; i < ListTag.Count; i++)
                            {
                                if(i != ListTag.Count - 1)
                                {
                                    <label class="form-control-static">@ListTag[i].Tag, </label>
                                }
                                else
                                {
                                    <label class="form-control-static">@ListTag[i].Tag</label>     
                                }
                            }
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>        

    @*@if (OriginalEmail != null)
{
    <div class="portlet box blue">
            <div class="portlet-title">
                <h2>Original Email</h2>
                <div class="row-fluid">
                    <div class="span3">
                        From: @OriginalEmail.From                     
                    </div>   
                    </div>     
                <div class="row-fluid">
                    <div class="span3">To: @OriginalEmail.To</div>        
                </div>
                 <div class="row-fluid">
                    <div class="span3">CC: @OriginalEmail.Cc</div>        
                </div>
                 <div class="row-fluid">
                    <div class="span3"><h4>@OriginalEmail.Subject</h4></div>        
                </div>    
            </div>
        <div class="portlet-body">
            <div class="">
                            @Html.Raw(@OriginalEmail.Body)
                <form action="/EmailTicket/EmailDetails" method="POST" class="row-fluid fr-linktoticket123">            
                    </form>
            </div>    
       </div>    
    </div>
}*@
    <p style="font-size:20px;">List emails linked to ticket <small>(expand to view all email history)</small></p>
    @foreach (var firstEmail in ListFirstEmail)
    {
        <div class="portlet box blue">
            <div class="portlet-title">
                <div class="caption">
                    <label>From: @firstEmail.From</label><br>
                                                         <label>Subject: @firstEmail.Subject</label>
                                                
                </div>
                <div class="tools">
                                    
                    <a href="javascript:;" class="expand"></a>
                </div>
                @if (Model.StatusID == 2)
                { 
                    <div class="row-fluid span3"> 
                        <a class="btn btn default" style="float:right;" onclick="ViewReplyEmail(@firstEmail.EmailID)" data-toggle="modal" href="#ViewReplyEmail">Send email</a>

                    </div>
                }
            </div>
            <div class="portlet-body" style="display: none;">
                <div class="row">
                    @foreach (var email in ConversionIDEmailHistory[(int)firstEmail.ConversionID])
                    {
                        <div class="col-md-12">
                            @if (email.IsIncommingEmail != true)
                            {
                                <div style="float:right; width:650px;" class="panel panel-info">
                                    <div class="panel-heading">
                                        <div class="span3">From: @email.From <label style="float:right">@email.Date</label></div>
                                        To: @email.To<br />
                                        Subject: @email.Subject
                                    </div>
                                    <div class="panel-body">@Html.Raw(@email.Body)</div>
                                </div>
                            }
                            else
                            {
                                <div style="float:left; width:650px;" class="panel panel-success">
                                    <div class="panel-heading">
                                        <div class="span3">From: @email.From <label style="float:right">@email.Date</label></div>
                                        To: @email.To<br />
                                        Subject: @email.Subject </div>
                                    <div class="panel-body">@Html.Raw(@email.Body)</div>
                                </div>
                            }
                        </div>
                    }
                </div>
                </div> 
        </div>
    }

